home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / sillyballs / sillyballs.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.8 KB  |  194 lines

  1. /*
  2.     File:        SillyBalls.c
  3.  
  4.     Contains:    This is a very simple sample program that demonstrates how to use Color 
  5.                 QuickDraw.  It is about two pages of code, and does nothing more than open
  6.                 a color window and draw randomly colored ovals in the window.
  7.     
  8.                 The purpose is to show how to get some initial results with Color QuickDraw.
  9.                 It is a complete program and is very short to be as clear as possible.
  10.         
  11.                 It does not have an Event Loop.  It is not fully functional in the sense that
  12.                 it does not do all the things you would expect a well behaved Macintosh 
  13.                 program to do, like size the window naturally, have an event loop, use menus, 
  14.                 etc.
  15.     
  16.                 See Sample and TESample for the general structure and MultiFinder techniques that
  17.                 we recommend that you use when building a new application.
  18.  
  19.                 purpose:    To demonstrate a simple color App using Color QuickDraw.
  20.                             It draws colored balls in a color window, then uses colored
  21.                             text inverted in the ball.  The ball location and color is Random.
  22.                                         
  23.                             The inverted Bob text was a Skippy Blair special concept,
  24.                             kept for obvious aesthetic reasons.
  25.                             
  26.     Written by: Bo3b Johnson    
  27.  
  28.     Copyright:    Copyright © 1988-1999 by Apple Computer, Inc., All Rights Reserved.
  29.  
  30.                 You may incorporate this Apple sample source code into your program(s) without
  31.                 restriction. This Apple sample source code has been provided "AS IS" and the
  32.                 responsibility for its operation is yours. You are not permitted to redistribute
  33.                 this Apple sample source code as "Apple sample source code" after having made
  34.                 changes. If you're going to re-distribute the source, we require that you make
  35.                 it clear in the source that the code was descended from Apple sample source
  36.                 code, but that you've made changes.
  37.  
  38.     Change History (most recent first):
  39.                 8/13/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  40.                 7/20/88         DJB                Converted to C
  41.                             MW                 -cut out some other program descriptions
  42.                 8/31/93 JA                    MW ** Metrowerks note **
  43.                                             All changed code by Metrowerks is commented by "//MW".
  44.                                             There is one type of modification to the original source:
  45.                                             • Added argument type and return type to function definitions.
  46.                                             In order to pass with extended error checking on.
  47. */
  48.  
  49.  
  50.  
  51. #include <Quickdraw.h>
  52. #include <Fonts.h>
  53. #include <Sound.h>
  54.  
  55. /* Constants */
  56. #define BallWidth        20
  57. #define BallHeight        20
  58. #define BobSize            8        /* Size of text in each ball */
  59.  
  60. /* Globals */
  61. Rect    windRect;
  62.     
  63. /* Prototypes */
  64. void Initialize(void);
  65. void NewBall(void);
  66.  
  67. // 
  68. //    Main body of program SillyBalls
  69. //
  70.  
  71. //MW specified argument and return type.
  72. void main(void)
  73. {
  74.     Initialize();
  75.     
  76.     do {
  77.         NewBall();
  78.     } while (!Button());
  79.     
  80. }
  81.  
  82. // 
  83. //    Initialize everything for the program, make sure we can run
  84. //
  85.  
  86. //MW specified argument and return type.
  87. void Initialize(void)
  88. {
  89.     WindowPtr    mainPtr;
  90.     OSErr        error;
  91.     SysEnvRec    theWorld;
  92.         
  93.     //
  94.     //    Test the computer to be sure we can do color.  
  95.     //    If not we would crash, which would be bad.  
  96.     //    If we can’t run, just beep and exit.
  97.     //
  98.  
  99.     error = SysEnvirons(1, &theWorld);
  100.     if (theWorld.hasColorQD == false) {
  101.         SysBeep(50);
  102.         ExitToShell();                    /* If no color QD, we must leave. */
  103.     }
  104.     
  105.     /* Initialize all the needed managers. */
  106.     InitGraf(&qd.thePort);
  107.     InitFonts();
  108.     InitWindows();
  109.     InitMenus();
  110.     TEInit();
  111.     InitDialogs(nil);
  112.     InitCursor();
  113.  
  114.     //
  115.     //    To make the Random sequences truly random, we need to make the seed start
  116.     //    at a different number.  An easy way to do this is to put the current time
  117.     //    and date into the seed.  Since it is always incrementing the starting seed
  118.     //    will always be different.  Don’t for each call of Random, or the sequence
  119.     //    will no longer be random.  Only needed once, here in the init.
  120.     //
  121.     GetDateTime((unsigned long*) &qd.randSeed);
  122.  
  123.     //
  124.     //    Make a new window for drawing in, and it must be a color window.  
  125.     //    The window is full screen size, made smaller to make it more visible.
  126.     //
  127.     windRect = qd.screenBits.bounds;
  128.     InsetRect(&windRect, 50, 50);
  129.     mainPtr = NewCWindow(nil, &windRect, "\pBob Land", true, documentProc, 
  130.                         (WindowPtr) -1, false, 0);
  131.         
  132.     SetPort(mainPtr);                        /* set window to current graf port */
  133.     TextSize(BobSize);                        /* smaller font for drawing. */
  134. }
  135.  
  136.  
  137. // 
  138. //    NewBall: make another ball in the window at a random location and color.
  139. //
  140.  
  141. //MW -specified argument and return type.-
  142. void NewBall(void)
  143. {
  144.     RGBColor    ballColor;
  145.     Rect        ballRect;
  146.     long int    newLeft,
  147.                 newTop;
  148.     
  149.     // 
  150.     //    Make a random new color for the ball.
  151.     //
  152.     ballColor.red   = Random();
  153.     ballColor.green = Random();
  154.     ballColor.blue  = Random();
  155.     
  156.     // 
  157.     //    Set that color as the new color to use in drawing.
  158.     //
  159.     RGBForeColor (&ballColor);
  160.  
  161.     //    
  162.     //    Make a Random new location for the ball, that is normalized to the window size.  
  163.     //    This makes the Integer from Random into a number that is 0..windRect.bottom
  164.     //    and 0..windRect.right.  They are normalized so that we don't spend most of our
  165.     //    time drawing in places outside of the window.
  166.     //
  167.     newTop = Random();    newLeft = Random();
  168.     newTop = ((newTop+32767) * windRect.bottom)/65536;
  169.     newLeft = ((newLeft+32767) * windRect.right)/65536;
  170.     SetRect(&ballRect, newLeft, newTop, newLeft+BallWidth, newTop+BallHeight);
  171.     
  172.     //
  173.     //    Move pen to the new location, and paint the colored ball.
  174.     //
  175.     MoveTo(newLeft, newTop);
  176.     PaintOval (&ballRect);
  177.     
  178.     //
  179.     //    Move the pen to the middle of the new ball position, for the text
  180.     //
  181.     MoveTo(ballRect.left + BallWidth/2 - BobSize, 
  182.         ballRect.top + BallHeight/2 + BobSize/2 -1);
  183.     
  184.     //    
  185.     //    Invert the color and draw the text there.  This won’t look quite right in 1 bit
  186.     //    mode, since the foreground and background colors will be the same.
  187.     //    Color QuickDraw special cases this to not invert the color, to avoid
  188.     //    invisible drawing.
  189.     //
  190.     InvertColor(&ballColor); 
  191.     RGBForeColor(&ballColor);
  192.     DrawString("\pBob");
  193. }
  194.